// pages/vehicle/vehicleFileItems.js
import eycService from "../../js/eycService";
import util from "../../utils/util";
import consts from "../../js/const";
import {routerFilter} from '../../utils/routerFilter';
import calcDate from "../../js/calcDate";
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
routerFilter({

    /**
     * 页面的初始数据
     */
    data: {
        carImgUrl: consts.getImageUrl('addCar.png'),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
            if(data){
                that.setData(data);
            }
        });
        that.isLogin(null, function () {
            that.searchEycVehicleFileByUserId();
        });
        util.promise('/baseConfig/searchPageConfigByTypeCde', {isRequestParam:true,pageCde:'vehicleFileItems',typeCde:'CAROUSEL',status:'FILE'}).then(function (res) {
            if (res.data) {
                that.setData({carousels:res.data});
            }
        });
    },
    searchEycVehicleFileByUserId: function () {
        let that = this;
        eycService.searchEycVehicleFileByUserId().then(function (res) {
            if (res.data) {
                let vehicleFiles = res.data;
                that.setData({vehicleFiles: vehicleFiles});
                vehicleFiles.forEach(function (vehicleFile) {
                    util.promise('/baseConfig/searchMatchInspRule', vehicleFile).then(function (res) {
                        if (res.data) {
                            if (vehicleFile.registerDt) {
                                vehicleFile.discreetDays = calcDate.calcDateDayDifference(calcDate.caleInspEndDate(new Date(vehicleFile.registerDt), res.data), new Date());
                            }
                            that.setData({vehicleFiles: vehicleFiles});
                        }
                    });
                    util.promise('/searchEycVehicleAvByPlateNum',vehicleFile.plateNum).then(function (res) {
                        if(res.data){
                            vehicleFile.price=res.data.avAmount;
                            that.setData({vehicleFiles: vehicleFiles});
                        }
                    });
                });
            }
        })
    },
    deleteVehicle(event) {
        let that = this;
        let vehicleFile = event.currentTarget.dataset.vehicleFile;
        Dialog.confirm({
            title: '删除爱车',
            message: '是否确认删除\n' + vehicleFile.plateNum + '车辆信息',
        }).then(() => {
            util.promise('/deleteEycVehicleFile', vehicleFile).then(res => {
                if (res.data) {
                    Toast('删除成功~');
                    that.searchEycVehicleFileByUserId();
                }
            });

        }).catch(() => {
            Dialog.close();
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    addVehicle(event) {
        let that = this;
        that.isLogin(null, function () {
            wx.navigateTo({
                url: '/pages/vehicle/addVehicle',
                events: {
                    refreshVehicleFiles: function (data) {
                        that.searchEycVehicleFileByUserId();
                    }
                },
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    // res.eventChannel.emit('acceptDataFromOpenerPage', {data: 'test'})
                }
            })
        });
    },
    setSelectVehicleFile(event){
        let that=this;
        let vehicleFile = event.currentTarget.dataset.vehicleFile;
        let vehicleFiles=that.data.vehicleFiles;
        vehicleFiles.forEach(function (vehicle) {
             if(vehicleFile.plateNum===vehicle.plateNum){
                 vehicle.isSelected=! vehicle.isSelected;
             }else{
                 vehicle.isSelected=false;
             }
        });
        that.setData({vehicleFiles: vehicleFiles});

    },
    selectVehicleFile: function (event) {
        let that=this;
        let vehicleFiles=that.data.vehicleFiles;
        vehicleFiles.forEach(function (vehicleFile) {
            if(vehicleFile.isSelected){
                if(vehicleFile.apc>9){
                    Toast('尊敬的用户，您的车辆业务暂未开放，可以通过客服系统咨询详情');
                    return;
                }
                const eventChannel = that.getOpenerEventChannel();
                eventChannel.emit('acceptVehicleFileFromItemsPage', vehicleFile);
                wx.navigateBack({delta: 1});
            }
        });
    },
    updateVehicleFile(event){
        let that=this;
        let vehicleFiles=that.data.vehicleFiles;
        vehicleFiles.forEach(function (vehicleFile) {
            if(vehicleFile.isSelected){
                wx.navigateTo({
                    url: '/pages/vehicle/addVehicle',
                    events: {
                        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                        acceptVehicleFileFromItemsPage: function (vehicleFile) {
                            that.refreshVehicleFile(vehicleFile);
                        }
                    },
                    success: function (res) {
                        res.eventChannel.emit('acceptDataFromOpenerPage', { vehicleFile: vehicleFile });
                    }
                });
            }
        });

    }
});